﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GestionWeb.Models;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using RESTServicesMov.Dominio;
using GestionWeb.Excepciones;


namespace GestionWeb.Controllers
{
    public class ConsultarSController : Controller
    {
      
     
        private List<ConsultaCS> ObtenerDatos(int idprod)
        {


            HttpWebRequest req = (HttpWebRequest)WebRequest.
             Create("http://localhost:50310/ConsultasCSs.svc/Consultas/" + idprod );
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string detalleJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<ConsultaCS> consLista = js.Deserialize<List<ConsultaCS>>(detalleJson);

            return consLista;
                 
          
        }


        public ActionResult IngresoCod()
        {
            return View();
        }


        public ActionResult Reporte()
        {
            return View();
        }


        [HttpPost]

        public ActionResult IngresoCod(FormCollection collection)
        {
    try
            {

                int id = int.Parse(collection["idProducto"]);
                
                 Session["consulta"] = ObtenerDatos(id);
                 List<ConsultaCS> model = (List<ConsultaCS>)Session["consulta"];
                return View("Reporte", model);
            }
          
                

                  catch (WebException e)
            {
                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader reader2 = new StreamReader(resError.GetResponseStream());
                string error = reader2.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Excepcion Ex = js.Deserialize<Excepcion>(error);

                //ModelState.AddModelError(String.Empty, e.Message);
                ModelState.AddModelError(String.Empty, Ex.Mensaje);
                return View();
                    }


        }

      

         }
}
